package com.example.finalwork.entity;


import org.springframework.http.HttpStatus;

public class ResponseMessage<T> {
    private Integer code; // 200, 500, 404
    private String message; // 请求成功
    private T data;

    // 成功响应
    public static <T> ResponseMessage success(T data) {
        return new ResponseMessage(200, "success", data);
    }
    public static <T> ResponseMessage success() {
        return new ResponseMessage(200, "success", null);
    }
    // 失败响应
    public static <T> ResponseMessage error(String message) {
        return new ResponseMessage(500, message, null);
    }
    // 构造函数
    public ResponseMessage(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // 获取响应代码
    public Integer getCode() {
        return code;
    }

    // 获取消息
    public String getMessage() {
        return message;
    }

    // 获取数据
    public T getData() {
        return data;
    }

    // 设置响应代码
    public void setCode(Integer code) {
        if (code != null) {
            this.code = code;
        } else {
            throw new IllegalArgumentException("Code cannot be null");
        }
    }

    // 设置消息
    public void setMessage(String message) {
        if (message != null && !message.trim().isEmpty()) {
            this.message = message;
        } else {
            throw new IllegalArgumentException("Message cannot be null or empty");
        }
    }

    // 设置数据
    public void setData(T data) {
        this.data = data; // 允许 data 为 null
    }
}
